<template>
  <div class="app-container">
    <div class="header">
      <el-input v-model="input" placeholder="请输入员工姓名" clearable @keyup.enter.native="getList">
        <i slot="prefix" class="el-input__icon el-icon-search" style="cursor: pointer" @click="getList"></i>
      </el-input>
      <el-button type="primary" icon="el-icon-plus" @click="$router.push({ name: 'AddEmployee' })">添加员工</el-button>
    </div>
    <el-table border :data="tableData" style="width: 100%">
      <el-table-column prop="name" label="员工姓名" width="180">
      </el-table-column>
      <el-table-column prop="username" label="账号" width="180">
      </el-table-column>
      <el-table-column prop="phone" label="手机号"> </el-table-column>
      <el-table-column prop="status" label="账号状态">
        <template v-slot="{ row }">
          {{ row.status ? "正常" : "已禁用" }}
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-link type="primary" @click="handleEdit(row)">编辑</el-link>
          <el-divider direction="vertical"></el-divider>
          <el-popconfirm title="确认调整该账号的状态?" @onConfirm="handleStatusChange(row)">
            <el-link type="danger" slot="reference">{{ row.status ? "禁用" : "启用" }}</el-link>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="footer">
      <el-pagination
      @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.page"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"/>
    </div>
    </div>
</template>

<script>
import { getEmployees, changeStatus } from '@/api/employee'
export default {
  data() {
    return {
      pageInfo: {
        page: 1,
        pageSize: 10
      },
      total: 0,
      input: '',
      tableData: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const {
        data: { records, total }
      } = await getEmployees({ ...this.pageInfo, name: this.input })
      this.tableData = records
      this.total = total
    },
    handleSizeChange(val) {
      this.pageInfo.page = 1
      this.pageInfo.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.pageInfo.page = val
      this.getList()
    },
    handleStatusChange(row) {
      changeStatus({ id: row.id, status: +!row.status }).then(() => {
        this.$message.success('改变状态成功')
        this.getList()
      })
    },
    handleEdit(row) {
      this.$router.push({
        name: 'EditEmployee',
        query: { isEdit: '1', id: row.id }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;

    .el-input {
      width: 250px;
    }
  }

  .footer {
    margin-top: 22px;
    display: flex;
    justify-content: center;
  }
}
</style>
